home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / zcie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  20.7 KB  |  617 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zcie.c */
  20. /* CIE color operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gxrefct.h"            /* early for gscie.h */
  25. #include "gxcolor.h"            /* for gscolor2.h */
  26. #include "gscspace.h"
  27. #include "gscolor2.h"
  28. #include "gscie.h"
  29. #include "alloc.h"
  30. #include "dict.h"
  31. #include "dparam.h"
  32. #include "estack.h"
  33. #include "save.h"
  34. #include "state.h"
  35. #include "store.h"        /* for make_null */
  36.  
  37. /* There are actually only two CIE-specific operators, */
  38. /* but CIE color dictionaries are so complex that */
  39. /* we handle the CIE case of setcolorspace here as well. */
  40.  
  41. /* Forward references */
  42. private int cache_colorrendering(P3(gs_cie_render *,
  43.   const ref_cie_render_procs *, gs_state *));
  44. private int cache_common(P4(gs_cie_common *, const ref_cie_procs *,
  45.   const ref_cie_render_procs *, gs_state *));
  46. #ifdef AMIGA
  47. extern int gx_install_CIEBasedABC(gs_color_space *, gs_state *);
  48. extern int gx_install_CIEBasedA(gs_color_space *, gs_state *);
  49. extern int gs_cie_render_init(gs_cie_render *);
  50. #endif
  51.  
  52. /* Elements in a CIEBasedABC color space dictionary: */
  53. static ref name_RangeABC;
  54. static ref name_DecodeABC;
  55. static ref name_MatrixABC;
  56. static ref name_RangeLMN;
  57. static ref name_DecodeLMN;
  58. static ref name_MatrixLMN;
  59. static ref name_WhitePoint;
  60. static ref name_BlackPoint;
  61. /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  62. static ref name_RangeA;
  63. static ref name_DecodeA;
  64. static ref name_MatrixA;
  65. /* (Additional) elements in a CIE color rendering dictionary: */
  66. static ref name_ColorRenderingType;
  67. static ref name_EncodeLMN;
  68. static ref name_EncodeABC;
  69. static ref name_MatrixPQR;
  70. static ref name_RangePQR;
  71. static ref name_TransformPQR;
  72. static ref name_RenderTable;
  73.  
  74. /* Empty procedures */
  75. static ref empty_procs[3];
  76.  
  77. /* Original CIE color space types */
  78. extern const gs_color_space_type
  79.     gs_color_space_type_CIEBasedABC,
  80.     gs_color_space_type_CIEBasedA;
  81. /* Redefined CIE color space types (that load the cache when installed) */
  82. gs_color_space_type
  83.     cs_type_zCIEBasedABC,
  84.     cs_type_zCIEBasedA;
  85. private cs_proc_install_cspace(cs_install_zCIEBasedABC);
  86. private cs_proc_install_cspace(cs_install_zCIEBasedA);
  87.  
  88. /* Initialization */
  89. private void
  90. zcie_init(void)
  91. {    static const names_def cien[] = {
  92.  
  93.     /* Elements in a CIEBasedABC color space dictionary: */
  94.        { "RangeABC", &name_RangeABC },
  95.        { "DecodeABC", &name_DecodeABC },
  96.        { "MatrixABC", &name_MatrixABC },
  97.        { "RangeLMN", &name_RangeLMN },
  98.        { "DecodeLMN", &name_DecodeLMN },
  99.        { "MatrixLMN", &name_MatrixLMN },
  100.        { "WhitePoint", &name_WhitePoint },
  101.        { "BlackPoint", &name_BlackPoint },
  102.  
  103.     /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  104.        { "RangeA", &name_RangeA },
  105.        { "DecodeA", &name_DecodeA },
  106.        { "MatrixA", &name_MatrixA },
  107.  
  108.     /* (Additional) elements in a CIE color rendering dictionary: */
  109.        { "ColorRenderingType", &name_ColorRenderingType },
  110.        { "EncodeLMN", &name_EncodeLMN },
  111.        { "EncodeABC", &name_EncodeABC },
  112.        { "MatrixPQR", &name_MatrixPQR },
  113.        { "RangePQR", &name_RangePQR },
  114.        { "TransformPQR", &name_TransformPQR },
  115.        { "RenderTable", &name_RenderTable },
  116.  
  117.     /* Mark the end of the initialized name list. */
  118.        names_def_end
  119.     };
  120.  
  121.     init_names(cien);
  122.  
  123.     /* Make the null (default) transformation procedures. */
  124.     make_const_array(&empty_procs[0], a_readonly + a_executable, 0, NULL);
  125.     make_const_array(&empty_procs[1], a_readonly + a_executable, 0, NULL);
  126.     make_const_array(&empty_procs[2], a_readonly + a_executable, 0, NULL);
  127.  
  128.     /* Create the modified color space types. */
  129.     cs_type_zCIEBasedABC = gs_color_space_type_CIEBasedABC;
  130.     cs_type_zCIEBasedABC.install_cspace = cs_install_zCIEBasedABC;
  131.     cs_type_zCIEBasedA = gs_color_space_type_CIEBasedA;
  132.     cs_type_zCIEBasedA.install_cspace = cs_install_zCIEBasedA;
  133.  
  134. }
  135.  
  136. /* ------ CIE setcolorspace ------ */
  137.  
  138. /* Get a 3-element range parameter from a dictionary. */
  139. #define dict_range3_param(op, pname, prange)\
  140.     dict_float_array_param(op, pname, 6, (float *)prange, (float *)&Range3_default)
  141. #define range3_ok 6
  142.  
  143. /* Get a 3x3 matrix parameter from a dictionary. */
  144. #define dict_matrix3_param(op, pname, pmat)\
  145.     dict_float_array_param(op, pname, 9, (float *)pmat, (float *)&Matrix3_default)
  146. #define matrix3_ok 9
  147.  
  148. /* Get an array of procedures from a dictionary. */
  149. /* We know count <= 3. */
  150. private int
  151. dict_proc_array_param(const ref *pdict, const ref *pname,
  152.   uint count, ref *pparray)
  153. {    ref *pvalue;
  154.     if ( dict_find(pdict, pname, &pvalue) > 0 )
  155.     {    uint i;
  156.         check_array(*pvalue);
  157.         if ( r_size(pvalue) != count )
  158.             return_error(e_rangecheck);
  159.         for ( i = 0; i < count; i++ )
  160.         {    ref proc;
  161.             array_get(pvalue, (long)i, &proc);
  162.             check_proc(proc);
  163.         }
  164.         *pparray = *pvalue;
  165.     }
  166.     else
  167.         make_const_array(pparray, a_readonly, count, &empty_procs[0]);
  168.     return 0;
  169. }
  170.  
  171. /* Get 3 procedures from a dictionary. */
  172. #define dict_proc3_param(op, pname, pparray)\
  173.     dict_proc_array_param(op, pname, 3, pparray)
  174.         
  175. /* Shared code for getting WhitePoint and BlackPoint values. */
  176. private int
  177. cie_points_param(const ref *pdref, gs_cie_wb *pwb)
  178. {    int code;
  179.     if ( (code = dict_float_array_param(pdref, &name_WhitePoint, 3, (float *)&pwb->WhitePoint, NULL)) != 3 ||
  180.          (code = dict_float_array_param(pdref, &name_BlackPoint, 3, (float *)&pwb->BlackPoint, (float *)&BlackPoint_default)) != 3
  181.        )
  182.         return (code < 0 ? code : e_rangecheck);
  183.     if ( pwb->WhitePoint.u <= 0 ||
  184.          pwb->WhitePoint.v != 1 ||
  185.          pwb->WhitePoint.w <= 0 ||
  186.          pwb->BlackPoint.u < 0 ||
  187.          pwb->BlackPoint.v < 0 ||
  188.          pwb->BlackPoint.w < 0
  189.        )
  190.         return_error(e_rangecheck);
  191.     return 0;
  192. }
  193.  
  194. /* Common code for the CIEBasedA[BC] cases of setcolorspace. */
  195. private int
  196. cie_lmnp_param(const ref *pdref, gs_cie_common *pcie, ref_cie_procs *pcprocs)
  197. {    int code;
  198.     if ( (code = dict_range3_param(pdref, &name_RangeLMN, &pcie->RangeLMN)) != range3_ok ||
  199.          (code = dict_proc3_param(pdref, &name_DecodeLMN, &pcprocs->DecodeLMN)) < 0 ||
  200.          (code = dict_matrix3_param(pdref, &name_MatrixLMN, &pcie->MatrixLMN)) != matrix3_ok ||
  201.          (code = cie_points_param(pdref, &pcie->points)) < 0
  202.        )
  203.         return (code < 0 ? code : e_rangecheck);
  204.     pcie->DecodeLMN = DecodeLMN_default;
  205.     return 0;
  206. }
  207.  
  208. /* Get the parameters of a CIEBasedABC color space. */
  209. /* This doesn't actually implement setcolorspace, */
  210. /* since it has to be usable for the base color space */
  211. /* of Separation, Indexed, and Pattern spaces as well. */
  212. int
  213. zcolorspace_CIEBasedABC(const ref *pdref, gs_color_space *pcs,
  214.   ref_cie_procs *pcprocs)
  215. {    gs_cie_abc *pcie;
  216.     int code;
  217.     check_type(*pdref, t_dictionary);
  218.     check_dict_read(*pdref);
  219.     rc_alloc_struct_0(pcie, gs_cie_abc, &alloc_memory_procs,
  220.               return_error(e_VMerror),
  221.               "setcolorspace(CIEBasedABC)");
  222.     if ( (code = dict_range3_param(pdref, &name_RangeABC, &pcie->RangeABC)) != range3_ok ||
  223.          (code = dict_proc3_param(pdref, &name_DecodeABC, &pcprocs->Decode.ABC)) < 0 ||
  224.          (code = dict_matrix3_param(pdref, &name_MatrixABC, &pcie->MatrixABC)) != matrix3_ok ||
  225.          (code = cie_lmnp_param(pdref, &pcie->common, pcprocs)) < 0
  226.        )
  227.     {    rc_free(pcie, &alloc_memory_procs,
  228.             "setcolorspace(CIEBasedABC)");
  229.         return (code < 0 ? code : e_rangecheck);
  230.     }
  231.     pcie->DecodeABC = DecodeABC_default;
  232.     pcs->params.abc = pcie;
  233.     pcs->type = &cs_type_zCIEBasedABC;
  234.     return 0;    /* installation will load the caches */
  235. }
  236.  
  237. /* Get the parameters of a CIEBasedA color space. */
  238. /* See above. */
  239. int
  240. zcolorspace_CIEBasedA(const ref *pdref, gs_color_space *pcs,
  241.   ref_cie_procs *pcprocs)
  242. {    gs_cie_a *pcie;
  243.     int code;
  244.     ref *pproc;
  245.     check_type(*pdref, t_dictionary);
  246.     check_dict_read(*pdref);
  247.     if ( (code = dict_find(pdref, &name_DecodeA, &pproc)) < 0 )
  248.         make_array(&pcprocs->Decode.A, a_readonly + a_executable, 0, NULL);
  249.     else
  250.     {    check_proc(*pproc);
  251.         pcprocs->Decode.A = *pproc;
  252.     }
  253.     rc_alloc_struct_0(pcie, gs_cie_a, &alloc_memory_procs,
  254.               return_error(e_VMerror),
  255.               "setcolorspace(CIEBasedA)");
  256.     if ( (code = dict_float_array_param(pdref, &name_RangeA, 2, (float *)&pcie->RangeA, (float *)&RangeA_default)) != 2 ||
  257.          (code = dict_float_array_param(pdref, &name_MatrixA, 3, (float *)&pcie->MatrixA, (float *)&MatrixA_default)) != 3 ||
  258.          (code = cie_lmnp_param(pdref, &pcie->common, pcprocs)) < 0
  259.        )
  260.     {    rc_free(pcie, &alloc_memory_procs,
  261.             "setcolorspace(CIEBasedA)");
  262.         return (code < 0 ? code : e_rangecheck);
  263.     }
  264.     pcie->DecodeA = DecodeA_default;
  265.     pcs->params.a = pcie;
  266.     pcs->type = &cs_type_zCIEBasedA;
  267.     return 0;    /* installation will load the caches */
  268. }
  269.  
  270. /* ------ CIE rendering dictionary ------ */
  271.  
  272. /* - currentcolorrendering <dict> */
  273. private int
  274. zcurrentcolorrendering(register os_ptr op)
  275. {    push(1);
  276.     *op = istate.colorrendering.dict;
  277.     return 0;
  278. }
  279.  
  280. /* <dict> setcolorrendering - */
  281. private int zsetcolorrendering_internal(P3(os_ptr, gs_cie_render *, ref_cie_render_procs *));
  282. private int
  283. zsetcolorrendering(register os_ptr op)
  284. {    int code;
  285.     es_ptr ep = esp;
  286.     gs_cie_render *pcie;
  287.     ref_cie_render_procs procs_old;
  288.     check_read_type(*op, t_dictionary);
  289.     check_dict_read(*op);
  290.     rc_alloc_struct_0(pcie, gs_cie_render, &alloc_memory_procs,
  291.               return_error(e_VMerror),
  292.               "setcolorrendering");
  293.     /* gs_setcolorrendering may refer to istate.colorrendering.procs. */
  294.     procs_old = istate.colorrendering.procs;
  295.     code = zsetcolorrendering_internal(op, pcie, &istate.colorrendering.procs);
  296.     if ( code < 0 )
  297.     {    rc_free(pcie, &alloc_memory_procs, "setcolorrendering");
  298.         istate.colorrendering.procs = procs_old;
  299.         esp = ep;
  300.         return code;
  301.     }
  302.     istate.colorrendering.dict = *op;
  303.     pop(1);
  304.     return (esp == ep ? 0 : o_push_estack);
  305. }
  306. private int
  307. zsetcolorrendering_internal(os_ptr op, gs_cie_render *pcie,
  308.   ref_cie_render_procs *pcprocs)
  309. {    int code;
  310.     int discard;
  311.     ref *pRT;
  312.     if ( (code = dict_int_param(op, &name_ColorRenderingType, 1, 1, 0, &discard)) < 0 ||
  313.          (code = dict_matrix3_param(op, &name_MatrixLMN, &pcie->MatrixLMN)) != matrix3_ok ||
  314.          (code = dict_proc3_param(op, &name_EncodeLMN, &pcprocs->EncodeLMN)) < 0 ||
  315.          (code = dict_range3_param(op, &name_RangeLMN, &pcie->RangeLMN)) != range3_ok ||
  316.          (code = dict_matrix3_param(op, &name_MatrixABC, &pcie->MatrixABC)) != matrix3_ok ||
  317.          (code = dict_proc3_param(op, &name_EncodeABC, &pcprocs->EncodeABC)) < 0 ||
  318.          (code = dict_range3_param(op, &name_RangeABC, &pcie->RangeABC)) != range3_ok ||
  319.          (code = cie_points_param(op, &pcie->points)) < 0 ||
  320.          (code = dict_matrix3_param(op, &name_MatrixPQR, &pcie->MatrixPQR)) != matrix3_ok ||
  321.          (code = dict_range3_param(op, &name_RangePQR, &pcie->RangePQR)) != range3_ok ||
  322.          (code = dict_proc3_param(op, &name_TransformPQR, &pcprocs->TransformPQR)) < 0
  323.        )
  324.         return (code < 0 ? code : e_rangecheck);
  325. #define rRT pcie->RenderTable
  326.     if ( dict_find(op, &name_RenderTable, &pRT) > 0 )
  327.     {    const ref *prte;
  328.         int i;
  329.         uint n2;
  330.         const ref *strings;
  331.         check_read_type(*pRT, t_array);
  332.         prte = pRT->value.const_refs;
  333.         check_type(prte[0], t_integer);
  334.         check_type(prte[1], t_integer);
  335.         check_type(prte[2], t_integer);
  336.         check_read_type(prte[3], t_array);
  337.         check_type(prte[4], t_integer);
  338.         if ( prte[0].value.intval <= 1 ||
  339.              prte[1].value.intval <= 1 ||
  340.              prte[2].value.intval <= 1 ||
  341.              !(prte[4].value.intval == 3 || prte[4].value.intval == 4)
  342.            )
  343.             return_error(e_rangecheck);
  344.         rRT.NA = prte[0].value.intval;
  345.         rRT.NB = prte[1].value.intval;
  346.         rRT.NC = prte[2].value.intval;
  347.         rRT.m = prte[4].value.intval;
  348.         n2 = rRT.m * rRT.NB * rRT.NC;
  349.         if ( r_size(pRT) != rRT.m + 5 || r_size(&prte[3]) != rRT.NA )
  350.             return_error(e_rangecheck);
  351.         strings = prte[3].value.const_refs;
  352.         for ( i = 0; i < rRT.NA; i++ )
  353.         {    const ref *prt2 = strings + i;
  354.             check_read_type(*prt2, t_string);
  355.             if ( r_size(prt2) != n2 )
  356.                 return_error(e_rangecheck);
  357.         }
  358.         prte += 5;
  359.         for ( i = 0; i < rRT.m; i++ )
  360.         {    const ref *prt2 = prte + i;
  361.             check_proc(*prt2);
  362.         }
  363.         rRT.table = (const byte **)alloc(rRT.NA, sizeof(byte *),
  364.                          "setcolorrendering(table)");
  365.         if ( rRT.table == 0 )
  366.             return_error(e_VMerror);
  367.         for ( i = 0; i < rRT.NA; i++ )
  368.             rRT.table[i] = strings[i].value.bytes;
  369.         make_const_array(&pcprocs->RenderTableT, a_readonly, rRT.m, prte + 5);
  370.     }
  371.     else
  372.     {    rRT.table = 0;
  373.         make_null(&pcprocs->RenderTableT);
  374.     }
  375. #undef rRT
  376.     pcie->EncodeLMN = Encode_default;
  377.     pcie->EncodeABC = Encode_default;
  378.     pcie->TransformPQR = TransformPQR_default;
  379.     pcie->RenderTable.T = RenderTableT_default;
  380.     code = cache_colorrendering(pcie, pcprocs, igs);
  381.     if ( code < 0 )
  382.         return code;
  383.     return gs_setcolorrendering(igs, pcie);
  384. }
  385.  
  386. /* ------ Internal routines ------ */
  387.  
  388. /* Import operators. */
  389. extern int
  390.   zfor(P1(os_ptr)),
  391.   zcvx(P1(os_ptr));
  392. extern int zexec(P1(os_ptr));
  393. /* Import accessors. */
  394. extern gx_cie_joint_caches *gx_currentciecaches(P1(gs_state *));
  395. extern char /*void*/ *gs_state_client_data(P1(gs_state *));
  396.  
  397. /* Forward declarations */
  398. private int
  399.   cie_cache_finish(P1(os_ptr)),
  400.   cie_exec_tpqr(P1(os_ptr)),
  401.   cie_tpqr_finish(P1(os_ptr));
  402.  
  403. /* Transform a set of ranges. */
  404. private void
  405. cie_transform_range(const gs_range3 *in, const gs_vector3 *col,
  406.   gs_range *out)
  407. {    float umin = col->u * in->u.rmin, umax = col->u * in->u.rmax;
  408.     float vmin = col->v * in->v.rmin, vmax = col->v * in->v.rmax;
  409.     float wmin = col->w * in->w.rmin, wmax = col->w * in->w.rmax;
  410.     float temp;
  411. #define swap(x, y) temp = x, x = y, y = temp
  412.     if ( umin > umax ) swap(umin, umax);
  413.     if ( vmin > vmax ) swap(vmin, vmax);
  414.     if ( wmin > wmax ) swap(wmin, wmax);
  415.     out->rmin = umin + vmin + wmin;
  416.     out->rmax = umax + vmax + wmax;
  417. #undef swap
  418. }
  419. private void
  420. cie_transform_range3(const gs_range3 *in, const gs_matrix3 *mat,
  421.   gs_range3 *out)
  422. {    cie_transform_range(in, &mat->cu, &out->u);
  423.     cie_transform_range(in, &mat->cv, &out->v);
  424.     cie_transform_range(in, &mat->cw, &out->w);
  425. }
  426.  
  427. /* Prepare to cache the values for one or more procedures. */
  428. private int
  429. cie_prepare_caches(const gs_range *domain, const ref *proc,
  430.   gx_cie_cache *pcache, int n)
  431. {    check_estack(n * 8);
  432.     for ( ; --n >= 0; domain++, proc++, pcache++, esp += 8 )
  433.     {    float diff = domain->rmax - domain->rmin;
  434.         float delta = diff / (gx_cie_cache_size - 1);
  435.         register es_ptr ep = esp;
  436.         /* Zero out the cache, since the gs level will try to */
  437.         /* access it before it has been filled. */
  438.         {    register float *pcv = &pcache->values[0];
  439.             register int i;
  440.             for ( i = 0; i < gx_cie_cache_size; i++, pcv++ )
  441.                 *pcv = 0.0;
  442.         }
  443.         pcache->base = domain->rmin - delta / 2;    /* so lookup will round */
  444.         pcache->factor = (delta == 0 ? 0 : 1 / delta);
  445.         pcache->is_identity = r_size(proc) == 0;
  446.         if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g\n",
  447.               (ulong)pcache, pcache->base, pcache->factor);
  448.         make_real(ep + 8, domain->rmin);
  449.         make_real(ep + 7, delta);
  450.         make_real(ep + 6, domain->rmax + delta / 2);
  451.         ep[5] = *proc;
  452.         r_clear_attrs(ep + 5, a_executable);
  453.         make_op_estack(ep + 4, zcvx);
  454.         make_op_estack(ep + 3, zfor);
  455.         make_op_estack(ep + 2, cie_cache_finish);
  456.         make_string(ep + 1, 0, sizeof(*pcache), (byte *)pcache);
  457.     }
  458.     return o_push_estack;
  459. }
  460. /* Prepare to cache the values for 3 procedures. */
  461. #define cie_prepare_cache3(d3,p3,c3)\
  462.   cie_prepare_caches((const gs_range *)(d3), p3, c3, 3)
  463.  
  464. /* Store the result of caching one procedure. */
  465. private int
  466. cie_cache_finish(os_ptr op)
  467. {    gx_cie_cache *pcache = (gx_cie_cache *)esp->value.bytes;
  468.     int code = num_params(op, gx_cie_cache_size, &pcache->values[0]);
  469.     if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g:\n",
  470.           (ulong)pcache, pcache->base, pcache->factor);
  471.     if ( code < 0 ) return code;
  472. #ifdef DEBUG
  473.     if ( debug_c('c') )
  474.     {    int i;
  475.         for ( i = 0; i < gx_cie_cache_size; i++ )
  476.           dprintf2("[c]cache[%3d]=%g\n", i, pcache->values[i]);
  477.     }
  478. #endif
  479.     pop(gx_cie_cache_size);
  480.     esp--;                /* pop pointer to cache */
  481.     return o_pop_estack;
  482. }
  483.  
  484. /* Install a CIE-based color space. */
  485.  
  486. private int
  487. cs_install_zCIEBasedABC(gs_color_space *pcs, gs_state *pgs)
  488. {    es_ptr ep = esp;
  489.     gs_cie_abc *pcie = pcs->params.abc;
  490.     const int_gstate *pigs = (int_gstate *)gs_state_client_data(pgs);
  491.     const ref_cie_procs *pcprocs = &pigs->colorspace.procs.cie;
  492.     int code = gx_install_CIEBasedABC(pcs, pgs);    /* former routine */
  493.     if ( code < 0 ) return code;
  494.     code = cie_prepare_cache3(&pcie->RangeABC, pcprocs->Decode.ABC.value.const_refs, &pcie->caches.DecodeABC[0]);
  495.     if ( code < 0 ||
  496.          (code = cache_common(&pcie->common, pcprocs, &pigs->colorrendering.procs, pgs)) < 0
  497.        )
  498.     {    esp = ep;
  499.         return code;
  500.     }
  501.     return o_push_estack;
  502. }
  503.  
  504. private int
  505. cs_install_zCIEBasedA(gs_color_space *pcs, gs_state *pgs)
  506. {    es_ptr ep = esp;
  507.     gs_cie_a *pcie = pcs->params.a;
  508.     const int_gstate *pigs = (int_gstate *)gs_state_client_data(pgs);
  509.     const ref_cie_procs *pcprocs = &pigs->colorspace.procs.cie;
  510.     int code = gx_install_CIEBasedA(pcs, pgs);    /* former routine */
  511.     if ( code < 0 ) return code;
  512.     code = cie_prepare_caches(&pcie->RangeA, &pcprocs->Decode.A, &pcie->caches.DecodeA, 1);
  513.     if ( code < 0 ||
  514.          (code = cache_common(&pcie->common, pcprocs, &pigs->colorrendering.procs, pgs)) < 0
  515.        )
  516.     {    esp = ep;
  517.         return code;
  518.     }
  519.     return o_push_estack;
  520. }
  521.  
  522. /* Cache the results of the color rendering procedures. */
  523. private int
  524. cache_colorrendering(gs_cie_render *pcie,
  525.   const ref_cie_render_procs *pcrprocs, gs_state *pgs)
  526. {    gs_range3 DomainLMN;
  527.     gs_range3 DomainABC;
  528.     es_ptr ep = esp;
  529.     static const gs_range ranges_01[4] =
  530.         { {0,1}, {0,1}, {0,1}, {0,1} };
  531.     int code = gs_cie_render_init(pcie);    /* sets PQR'*LMN */
  532.     if ( code < 0 ) return code;
  533.     cie_transform_range3(&pcie->RangePQR, &pcie->MatrixPQR_inverse_LMN, &DomainLMN);
  534.     cie_transform_range3(&pcie->RangeLMN, &pcie->MatrixABC, &DomainABC);
  535.     if ( (code = cie_prepare_cache3(&DomainLMN, pcrprocs->EncodeLMN.value.const_refs, &pcie->caches.EncodeLMN[0])) < 0 ||
  536.          (code = cie_prepare_cache3(&DomainABC, pcrprocs->EncodeABC.value.const_refs, &pcie->caches.EncodeABC[0])) < 0 ||
  537.          (pcie->RenderTable.table != 0 &&
  538.           (code = cie_prepare_caches(ranges_01, pcrprocs->RenderTableT.value.const_refs, &pcie->caches.RenderTableT[0], pcie->RenderTable.m)) < 0)
  539.        )
  540.     {    esp = ep;
  541.         return code;
  542.     }
  543.     /* gs_setcolorrendering reinstalls the color space, */
  544.     /* which reloads the joint caches if needed. */
  545.     return o_push_estack;
  546. }
  547.  
  548. /* Common cache code */
  549. private int
  550. cache_common(gs_cie_common *pcie, const ref_cie_procs *pcprocs,
  551.   const ref_cie_render_procs *pcrprocs, gs_state *pgs)
  552. {    int code = cie_prepare_cache3(&pcie->RangeLMN,
  553.                       pcprocs->DecodeLMN.value.const_refs,
  554.                       &pcie->caches.DecodeLMN[0]);
  555.     const gs_cie_render *pcier = gs_currentcolorrendering(pgs);
  556.     /* The former installation procedures have allocated */
  557.     /* the joint caches and filled in points_sd. */
  558.     gx_cie_joint_caches *pjc = gx_currentciecaches(pgs);
  559.     ref pqr_procs;
  560. #define pqr_refs pqr_procs.value.refs
  561.     int i;
  562.     if ( code < 0 ) return code;
  563.     if ( pcier == 0 ) return 0;    /* cache is not used */
  564.     check_estack(2);
  565.     code = alloc_array(&pqr_procs, a_readonly, 3*(1+3+4*6), "cie_cache_common");
  566.     if ( code < 0 ) return code;
  567.     /* Make sure we deallocate the procs when we're done. */
  568.     push_op_estack(cie_tpqr_finish);
  569.     *++esp = pqr_procs;
  570.     for ( i = 0; i < 3; i++ )
  571.     {    ref *p = pqr_refs + 3 + (3+4*6) * i;
  572.         const float *ppt = (float *)&pjc->points_sd;
  573.         int j;
  574.         make_array(pqr_refs + i, a_readonly + a_executable, 3, p);
  575.         make_array(p, a_readonly, 4*6, p + 3);
  576.         p[1] = pcrprocs->TransformPQR.value.refs[i];
  577.         make_oper(p + 2, 0, cie_exec_tpqr);
  578.         for ( j = 0, p += 3; j < 4*6; j++, p++, ppt++ )
  579.             make_real(p, *ppt);
  580.     }
  581.     return cie_prepare_cache3(&pcier->RangePQR,
  582.                   pqr_procs.value.const_refs,
  583.                   &pjc->TransformPQR[0]);
  584. }
  585.  
  586. /* Private operator to shuffle arguments for the TransformPQR procedure: */
  587. /* v [ws wd bs bd] proc -> ws wd bs bd v proc + exec */
  588. private int
  589. cie_exec_tpqr(register os_ptr op)
  590. {    const ref *ppt = op[-1].value.const_refs;
  591.     int i;
  592.     push(3);
  593.     *op = op[-3];        /* proc */
  594.     op[-1] = op[-5];    /* v */
  595.     for ( i = 0; i < 4; i++ )
  596.         make_const_array(op - 5 + i, a_readonly, 6, ppt + i * 6);
  597.     return zexec(op);
  598. }
  599.  
  600. /* Private operator to free procs array. */
  601. private int
  602. cie_tpqr_finish(register os_ptr op)
  603. {    alloc_free_array(op, "cie_tpqr_finish");
  604.     return 0;
  605. }
  606.  
  607. /* ------ Initialization procedure ------ */
  608.  
  609. op_def zcie_op_defs[] = {
  610.     {"0currentcolorrendering", zcurrentcolorrendering},
  611.     {"1setcolorrendering", zsetcolorrendering},
  612.     {"0%cie_cache_finish", cie_cache_finish},
  613.     {"3%cie_exec_tpqr", cie_exec_tpqr},
  614.     {"1%cie_tpqr_finish", cie_tpqr_finish},
  615.     op_def_end(zcie_init)
  616. };
  617.